package com.jmu.content.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jmu.constant.StatusConstant;
import com.jmu.content.dto.ChargePilePageDTO;
import com.jmu.content.mapper.ChargePileMapper;
import com.jmu.content.pojo.ChargePile;
import com.jmu.content.pojo.ChargeStation;
import com.jmu.content.pojo.PriceList;
import com.jmu.content.result.PageResult;
import com.jmu.content.service.ChargePileService;
import com.jmu.content.service.ChargeStationService;
import com.jmu.content.service.PriceListService;
import com.jmu.content.vo.ChargePileAdminVO;
import com.jmu.content.vo.ChargePileUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fems
 * @since 2024-05-06
 */
@Service
public class ChargePileServiceImpl extends ServiceImpl<ChargePileMapper, ChargePile> implements ChargePileService {


    @Autowired
    ChargeStationService chargeStationService;

    @Autowired
    PriceListService priceListService;

    @Override
    public PageResult pageQuery(ChargePilePageDTO dto) {
        Page<ChargePile> page = PageHelper.startPage(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<ChargePile> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(dto.getStationId()!=null,ChargePile::getStationId,dto.getStationId());
        queryWrapper.eq(dto.getStatus()!=null,ChargePile::getStatus,dto.getStatus());
        queryWrapper.eq(dto.getPileId()!=null,ChargePile::getId,dto.getPileId());
        List<ChargePile> list = this.list(queryWrapper);
        List<ChargePileAdminVO> voList = new ArrayList<>();
        list.stream().forEach(chargePile -> {
            Long stationId = chargePile.getStationId();
            ChargeStation station = chargeStationService.getById(stationId);
            ChargePileAdminVO chargePileVO = new ChargePileAdminVO();
            BeanUtils.copyProperties(chargePile,chargePileVO);
            chargePileVO.setStationName(station.getName());
            voList.add(chargePileVO);
        });
        PageInfo pageInfo = new PageInfo(voList);
        return new PageResult(page.getTotal(),pageInfo.getList());
    }

    @Override
    public List<ChargePileUserVO> getByIdBatchPrice(Long id) {
        LambdaQueryWrapper<ChargePile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChargePile::getStationId,id);
        queryWrapper.eq(ChargePile::getIsLocked, StatusConstant.PILE_UNLOCKED);

        //执行查找
        List<ChargePile> list = this.list(queryWrapper);
        List<ChargePileUserVO> voList = list.stream().map(item -> {
            ChargePileUserVO vo = new ChargePileUserVO();
            BeanUtils.copyProperties(item, vo);
            vo.setPrice(getPrice(item.getStationId()));
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

    public BigDecimal getPrice(Long stationId){
        LambdaQueryWrapper<PriceList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PriceList::getStationId,stationId);
        queryWrapper.orderByAsc(PriceList::getPrice);
        List<PriceList> list = priceListService.list(queryWrapper);
        if (list == null || list.size() == 0){
            return new BigDecimal(1.0);
        }
        return list.get(0).getPrice();
    }

}
